package com.bosssoft.trainee.controller;

import cn.hutool.json.JSONObject;
import com.bosssoft.trainee.common.Result;
import com.bosssoft.trainee.entity.po.Menus;
import com.bosssoft.trainee.entity.po.Route;
import com.bosssoft.trainee.service.MenuService;
import com.bosssoft.trainee.service.MyMenusService;
import com.bosssoft.trainee.service.RouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author liuqiang
 * @date 2022/7/18 21:03
 */
@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    MenuService menuService;
    @Autowired
    RouteService routeService;
    @Autowired
    MyMenusService myMenusService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 路由信息
     * @param id id 角色id
     * @return result
     */
    @GetMapping("/list")
    public Result<Object> menuInfo(String id){
        //返回的list
        List<Route> list = routeService.getRoutesByRole(Integer.valueOf(id));
        //meta数据
        Map<String,Object> meta = new HashMap<>();
        //角色
        List<String> roles = new ArrayList<>();
        //子路由
        List<Map<String,Object>> child = new ArrayList<>();
        List<Map<String,Object>> menuList = new ArrayList<>();

        JSONObject jsonObject = new JSONObject();
        log.info("路由信息：{}",list);
        if(list.isEmpty()){
            return null;
        }else {
            //角色权限
            roles.add(id.toString());
            for(Route route : list){
                if(route.getRouteParentId()==0){
                    Map<String,Object> map = routeService.getRoutesMap(route);
                    meta = routeService.getMetaMap(route);

                    meta.put("roles",roles);
                    map.put("meta",meta);
                    map.put("children",child);

                    menuList.add(map);
                }
            }
            List<Map<String,Object>> child1 = new ArrayList<>();
            for(int i =2;i<=3;++i){
                Map<String,Object> map1 = routeService.getRoutesMap(routeService.getById(i));
                Map<String,Object> meta1 = routeService.getMetaMap(routeService.getById(i));
                meta1.put("roles",roles);
                map1.put("meta",meta1);
                child1.add(map1);
            }
            menuList.get(0).put("children",child1);

            List<Map<String,Object>> child2 = new ArrayList<>();
            Map<String,Object> map2 = routeService.getRoutesMap(routeService.getById(5));
            Map<String,Object> meta2 = routeService.getMetaMap(routeService.getById(5));
            meta2.put("roles",roles);
            map2.put("meta",meta2);
            map2.remove("redirect");
            child2.add(map2);
            menuList.get(1).put("children",child2);



            jsonObject.set("routers",menuList);
            return Result.success(jsonObject);
        }
    }

    /**
     * 获取所有的路由信息
     * @return
     */
    @GetMapping("/list1")
    public Result<Object> menuList(){
//        List<Menus> menusList = myMenusService.getMenus(Integer.valueOf(id));
        List<Menus> menusList = myMenusService.list();
        //全部的路由信息
        List<Route> routeList = routeService.list();
        //json数据
        JSONObject jsonObject = new JSONObject();
        //meta数据
        Map<String,Object> meta = new HashMap<>();
        //角色
        List<String> roles = new ArrayList<>();
        //子路由
        List<Map<String,Object>> child = new ArrayList<>();
        //父路由
        List<Route> parent = new ArrayList<>();
        //最终返回的路由映射表
        List<Map<String,Object>> menuList = new ArrayList<>();
        //我们得出的routes-list
//        List<Route> list = new ArrayList<>();
//        for(Menus menus : menusList){
//            for(Route route : routeList){
//                if(Objects.equals(menus.getRouteId(), route.getRouteId())){
//                    list.add(route);
//                }
//            }
//        }
        //遍历整个角色所对应的route-list
        roles.add("0");
        //list
        for(Route route : routeList){
            //父节点先处理
            if(route.getRouteParentId()==0){
                parent.add(route);
                Map<String,Object> map = routeService.getRoutesMap(route);
                meta = routeService.getMetaMap(route);

                meta.put("roles",roles);
                map.put("meta",meta);
                map.put("children",child);
                map.put("routeId",route.getRouteId());

                menuList.add(map);
            }
        }
//        Route route1 : parent
        for(Map<String,Object>map1 : menuList){
            //子路由
            List<Map<String,Object>> child1 = new ArrayList<>();
            //list
            for(Route route : routeList){
                if(map1.get("routeId")==route.getRouteParentId()){
                    Map<String,Object> map = routeService.getRoutesMap(route);
                    meta = routeService.getMetaMap(route);
                    meta.put("roles",roles);
                    map.put("meta",meta);
                    child1.add(map);
                }
            }
            map1.put("children",child1);
            map1.remove("routeId");
        }

        jsonObject.set("routers",menuList);

        return Result.success(jsonObject);
    }


    /**
     * 根据角色id返回对应路由
     * @param id
     * @return
     */
    @GetMapping("/list2")
    public Result<Object> menusInfo(String id){
        List<Menus> menusList = myMenusService.getMenus(Integer.valueOf(id));
        List<Route> routeList = routeService.list();
        //json数据
        JSONObject jsonObject = new JSONObject();
        //meta数据
        Map<String,Object> meta = new HashMap<>();
        //角色
        List<String> roles = new ArrayList<>();
        //子路由
        List<Map<String,Object>> child = new ArrayList<>();
        //父路由
        List<Route> parent = new ArrayList<>();
        //最终返回的路由映射表
        List<Map<String,Object>> menuList = new ArrayList<>();
        //我们得出的routes-list
        List<Route> list = new ArrayList<>();
        for(Menus menus : menusList){
            for(Route route : routeList){
                if(Objects.equals(menus.getRouteId(), route.getRouteId())){
                    list.add(route);
                }
            }
        }
        //遍历整个角色所对应的route-list
        roles.add(id);
        for(Route route : list){
            //父节点先处理
            if(route.getRouteParentId()==0){
                parent.add(route);
                Map<String,Object> map = routeService.getRoutesMap(route);
                meta = routeService.getMetaMap(route);

                meta.put("roles",roles);
                map.put("meta",meta);
                map.put("children",child);
                map.put("routeId",route.getRouteId());

                menuList.add(map);
            }
        }
//        Route route1 : parent
        for(Map<String,Object>map1 : menuList){
            //子路由
            List<Map<String,Object>> child1 = new ArrayList<>();
            for(Route route : list){
                if(map1.get("routeId")==route.getRouteParentId()){
                    Map<String,Object> map = routeService.getRoutesMap(route);
                    meta = routeService.getMetaMap(route);
                    meta.put("roles",roles);
                    map.put("meta",meta);
                    child1.add(map);
                }
            }
            map1.put("children",child1);
            map1.remove("routeId");
        }

        jsonObject.set("routers",menuList);

        return Result.success(jsonObject);
    }


    /**
     *
     * @param id 角色id
     * @param list 路由信息的路径
     * @return 统一结果
     */
    @PostMapping("/updateRoute")
    public Result<Object> updateRoute(String id,@RequestBody List<String> list){

        log.info("进入updateRoute:{}",id);
        //所需的route列表
        List<Route> routeList = routeService.getRoutesByPath(list);
        //先放子路由？
        List<Route> routeList1 = new ArrayList<>();
        //所需的route列表的id列表
        List<Integer> routeIdList = new ArrayList<>();
        for(Route route : routeList){
            if(route.getRouteParentId()!=0){
                routeList1.add(route);
            }
        }
        for(Route route1 : routeList1){
            boolean s = false;
            Integer parentId = 0;
            for(Route route : routeList){
                //父路由存在
                if(Objects.equals(route1.getRouteParentId(), route.getRouteId())){
                    s = false;
                    break;
                }else {
                    parentId = route1.getRouteParentId();
//                    log.info("当前的parentId:{}",parentId);
                    s = true;
                }
            }
            //父路由不存在，则补充
            if(s){
                Route route2 = routeService.getRouteByParentId(parentId);
//                log.info("补充的路由信息:{}",route2);
                routeList.add(route2);
            }
        }

        for(Route route : routeList){
            routeIdList.add(route.getRouteId());
        }
        log.info("路由id列表:{}",routeIdList);
        myMenusService.saveRoutes(Integer.valueOf(id),routeIdList);
        return Result.success();
    }
}
